﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrmHotel
{
    public partial class FrmJieZhang : Form
    {
        public FrmJieZhang()
        {
            InitializeComponent();
        }
        public int id;//接收房间号
        public string Money = "";
        decimal s1 = 0;//押金
        public int Hao;//接收会员号
        string zongMoney = "";
        private void FrmJieZhang_Load(object sender, EventArgs e)
        {
            string sql2 = "select * from  PayClass";
            DataTable dt2 = DBHelper.SelectForTable(sql2);
            DataRow dr2 = dt2.NewRow();
            dr2["PayId"] = 0;
            dr2["PayName"] = "付款方式";
            dt2.Rows.InsertAt(dr2, 0);
            this.comboBox1.DisplayMember = "PayName";
            this.comboBox1.ValueMember = "PayId";
            this.comboBox1.DataSource = dt2;
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView2.AutoGenerateColumns = false;
            Satainfo();
            Liov();
            string sql = string.Format("select sum(CsPrice) from Consume where CsoRoom={0}", id);
             zongMoney =DBHelper.SelectOne(sql).ToString();//消费总额
            if (string.IsNullOrEmpty(zongMoney))
            {
                zongMoney = "0";
                this.label_XFmoney.Text = zongMoney;
                this.label4.Text = "200.00";
                return;
            }
            else
            {
                this.label_XFmoney.Text = zongMoney;
                decimal sss = Convert.ToDecimal(zongMoney);
                if (s1>sss)
                {
                    decimal s3 = Convert.ToDecimal(s1 - sss);
                    this.label4.Text = s3.ToString();

                }
                else if (s1 < sss)
                {
                    decimal s3 = Convert.ToDecimal(sss - s1);
                    this.label3.Text = s3.ToString();
                }
                
            }
            Fangshi();//支付明细
            
        }
       
        
        public void Satainfo()
        {
            string sql = string.Format("select * from CetaeInfo c,RoomInfo r,Room m where m.RoomId=r.RmRoom and r.RmId=c.CeFangHao and c.CeFangHao={0}",id);
            SqlDataReader reader = DBHelper.SelectMore(sql);
            if (reader.Read())
            {
                this.label_fanghao.Text = reader["RmId"].ToString();
                this.label_name.Text = reader["CeName"].ToString();
                this.label_yajinMoney.Text = reader["RmDpst"].ToString();
                s1 = Convert.ToDecimal(reader["RmDpst"]); 
            }
            reader.Close();
        }
        public void Liov()
        {
            string sql = string.Format("select * from Consume c,Commodity y where y.CyId=c.SsoName and c.CsoRoom={0}",id);
            this.dataGridView2.DataSource = DBHelper.SelectForTable(sql);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int fu = Convert.ToInt32(comboBox1.SelectedValue);
            string q= this.label3.Text;
            decimal q1=Convert.ToDecimal(q);
            if (q1==0)
            {
                MessageBox.Show("无支付金额！");
                return;
            }
            int sum = Convert.ToInt32(this.numericUpDown1.Value);
          
            if (sum==0)
            {
                MessageBox.Show("请输入支付金额！"); 
                return;
            }
            string sss=this.label6.Text;
            if (sss.Equals("——"))
            {
                if (fu <= 0)
                {
                    MessageBox.Show("请选择支付方式！");
                    return;
                }
            }  
                Fangshi();
        }
        public void Fangshi()
        {
            int fu = Convert.ToInt32(comboBox1.SelectedValue);
            string q = this.label3.Text;
            int sum = Convert.ToInt32(this.numericUpDown1.Value);
            decimal q1 = Convert.ToDecimal(q);
            if (!string.IsNullOrEmpty(label16.Text))
            {
              
                decimal k = Convert.ToDecimal(this.label3.Text);
                decimal k1 = Convert.ToDecimal(this.numericUpDown1.Value);
                decimal k2 = Convert.ToDecimal(k - k1);
                decimal k3 = Convert.ToDecimal(k1 - k);
                if (!label5.Text.Equals("——"))
                {
                    decimal yve = Convert.ToDecimal(label5.Text);//会员余额
                    decimal k4 = Convert.ToInt32(yve - k1);

                    if (k1 > yve)
                    {
                        MessageBox.Show("您会员的余额不足！");
                        return;
                    }
                    else
                    {
                        label5.Text = k4.ToString();
                    }
                }
                this.label3.Text = k2.ToString();
                double pp = Convert.ToDouble(this.label3.Text);
                Class1.xfmoney = Convert.ToDouble(pp);
                if (k1 > k)
                {
                    this.label3.Text = "0.00";
                    this.label4.Text = k3.ToString();
                    
                }

                string sql1 = string.Format("insert into ZhiFuMx values({0},{1})", fu, sum);
                int result = DBHelper.ExecuteUpdate(sql1);
            }
            
            this.dataGridView1.AutoGenerateColumns = false;
            string sql = string.Format("select * from ZhiFuMx z,PayClass p where p.PayId=z.Zmd");
            this.dataGridView1.DataSource = DBHelper.SelectForTable(sql);

        }
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
        }
        private void button6_Click(object sender, EventArgs e)
        {
            huiyuansata();
           // Fangshi();
        }
        public void huiyuansata()
        {
            Class1.yingfum =label3.Text;
            FrmHuiYuanJieZhang fu = new FrmHuiYuanJieZhang();
            fu.ShowDialog();
            Fangshi();
            if (Class1.zhuangtai == 1)
            {
                //button3.Enabled = false;
                comboBox1.Enabled = false;
                string sql = String.Format("select * from VipInfo a inner join VipType b  on a.VpKaType=b.VipId where a.VpId={0}", Class1.bianhao);
                SqlDataReader read = DBHelper.SelectMore(sql);
                if (read.Read())
                {
                    label16.Text = read["VpName"].ToString();
                    label6.Text = read["TypeName"].ToString();
                    label5.Text = read["VpMoney"].ToString();
                    label13.Text = read["ZheKou"].ToString();
                }
                read.Close();
            }
            if (!label5.Text.Equals("——"))
            {
                decimal yve = Convert.ToDecimal(label5.Text);//会员余额
                decimal zhekou = Convert.ToDecimal(label13.Text);//会员折扣
                decimal zhifu = Convert.ToDecimal(label3.Text);//支付金额
                decimal lingqian = Convert.ToDecimal(label4.Text);//找零
                double cheng = Convert.ToDouble(zhifu * zhekou);//除以折扣
                if (zhifu != 0)
                {
                    label3.Text = cheng.ToString();
                }
               
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

            string sql = "delete from  ZhiFuMx  where Zmd=1";
            int s = DBHelper.ExecuteUpdate(sql);
            string sql1 = "delete from ZhiFuMx  where Zmd=2";
            int s1= DBHelper.ExecuteUpdate(sql1);
            string sql2 = "delete from ZhiFuMx  where Zmd=3";
            int s2 = DBHelper.ExecuteUpdate(sql2);
            string sql3 = "delete from ZhiFuMx  where Zmd=4";
            int s3 = DBHelper.ExecuteUpdate(sql3);
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            string q = this.label3.Text;
            decimal q1 = Convert.ToDecimal(q);
            if (q1 > 0)
            {
                MessageBox.Show("付款金额不足！不能结账！");
                return;
            }
            else
            {
                string yve = label5.Text;//会员余额
                if (!yve.Equals("——"))
                {
                    decimal ppp = Convert.ToDecimal(label13.Text);//会员余额
                    decimal zhekou = Convert.ToDecimal(label13.Text);//会员折扣
                    decimal zhifu = Convert.ToDecimal(label3.Text);//支付金额
                    decimal lingqian = Convert.ToDecimal(label4.Text);//找零
                    double jia = Convert.ToDouble(ppp + lingqian);//零钱加上余额

                    string sql3 = string.Format("update VipInfo set VpMoney={0} where VpId={1}", jia, Class1.bianhao);
                    int result = DBHelper.ExecuteUpdate(sql3);
                }
                        string sql = string.Format("update roominfo set rmstate=4 where rmid={0}", id);
                        int ee = DBHelper.ExecuteUpdate(sql);

                        string name = label_name.Text;
                        string sql2 = string.Format("select CeId from CetaeInfo where CeName='{0}'", name);
                        string r = DBHelper.SelectOne(sql2).ToString();

                        string sql1 = string.Format("update  checkinfo set ckstate=3 where Ckname={0}", r);
                        int r1 = DBHelper.ExecuteUpdate(sql1);

                        string qwe = string.Format("delete from Consume  where CsoRoom={0}", id);
                        int r2 = DBHelper.ExecuteUpdate(qwe);

                        if (ee>0&&r1>0)
                        {
                             MessageBox.Show("结账成功！");
                             this.Close();
                        }
                        else
                        {
                            MessageBox.Show("结账失败！");
                        }
            }
        }
    }
}
